
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Django入门与实践-第16章：用户登录 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="DjangoORM0.html" />
    
    
    <link rel="prev" href="Authentication1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="GettingStarted.html">
            
                <a href="GettingStarted.html">
            
                    
                    Django入门与实践-第1章：环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="GettingStarted-2.html">
            
                <a href="GettingStarted-2.html">
            
                    
                    Django入门与实践-第2章：创建项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="gettingstarted-3.html">
            
                <a href="gettingstarted-3.html">
            
                    
                    Django入门与实践-第3章：Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Fundamentals-1.html">
            
                <a href="Fundamentals-1.html">
            
                    
                    Django入门与实践-第4章：系统设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Fundamentals-2.html">
            
                <a href="Fundamentals-2.html">
            
                    
                    Django入门与实践-第5章：模型设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="Fundamentals-3-0.html">
            
                <a href="Fundamentals-3-0.html">
            
                    
                    Django入门与实践-第6章：第一个视图函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="Fundamentals-3-1.html">
            
                <a href="Fundamentals-3-1.html">
            
                    
                    Django入门与实践-第7章：模板引擎设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Fundamentals-3-1-1.html">
            
                <a href="Fundamentals-3-1-1.html">
            
                    
                    Django入门与实践-第8章：第一个单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="Fundamentals-3-2.html">
            
                <a href="Fundamentals-3-2.html">
            
                    
                    Django入门与实践-第9章：静态文件设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="Fundamentals-4.html">
            
                <a href="Fundamentals-4.html">
            
                    
                    Django入门与实践-第10章：Django Admin 介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="AdvancedConcepts.html">
            
                <a href="AdvancedConcepts.html">
            
                    
                    Django入门与实践-第11章：URL 分发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="AdvancedConcept2.html">
            
                <a href="AdvancedConcept2.html">
            
                    
                    Django入门与实践-第12章：复用模板
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="AdvancedConcept3.html">
            
                <a href="AdvancedConcept3.html">
            
                    
                    Django入门与实践-第13章：表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Authentication0.html">
            
                <a href="Authentication0.html">
            
                    
                    Django入门与实践-第14章：用户注册
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Authentication1.html">
            
                <a href="Authentication1.html">
            
                    
                    Django入门与实践-第15章：用户注销
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.17" data-path="Authentication2.html">
            
                <a href="Authentication2.html">
            
                    
                    Django入门与实践-第16章：用户登录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="DjangoORM0.html">
            
                <a href="DjangoORM0.html">
            
                    
                    Django入门与实践-第17章：保护视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="DjangoORM1.html">
            
                <a href="DjangoORM1.html">
            
                    
                    Django入门与实践-第18章：实现主题回复列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="DjangoORM2.html">
            
                <a href="DjangoORM2.html">
            
                    
                    Django入门与实践-第19章：实现主题回复功能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="DjangoORM3.html">
            
                <a href="DjangoORM3.html">
            
                    
                    Django入门与实践-第20章：查询结果集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="DjangoORM.html">
            
                <a href="DjangoORM.html">
            
                    
                    Django入门与实践-第21章：Django数据迁移
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="ClassBasedViews0.html">
            
                <a href="ClassBasedViews0.html">
            
                    
                    Django入门与实践-第22章：基于类的视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="ClassBasedViews1.html">
            
                <a href="ClassBasedViews1.html">
            
                    
                    Django入门与实践-第23章：分页实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="ClassBasedViews2.html">
            
                <a href="ClassBasedViews2.html">
            
                    
                    Django入门与实践-第24章：我的账户
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="ClassBasedViews3.html">
            
                <a href="ClassBasedViews3.html">
            
                    
                    Django入门与实践-第25章：Markdown支持
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="ClassBasedViews4.html">
            
                <a href="ClassBasedViews4.html">
            
                    
                    Django入门与实践-第26章：个性化设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="Deployment.html">
            
                <a href="Deployment.html">
            
                    
                    Django入门与实践-第27章：项目部署
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Django入门与实践-第16章：用户登录</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="django&#x5165;&#x95E8;&#x4E0E;&#x5B9E;&#x8DF5;-&#x7B2C;16&#x7AE0;&#xFF1A;&#x7528;&#x6237;&#x767B;&#x5F55;">Django&#x5165;&#x95E8;&#x4E0E;&#x5B9E;&#x8DF5;-&#x7B2C;16&#x7AE0;&#xFF1A;&#x7528;&#x6237;&#x767B;&#x5F55;</h1>
<p>&#x9996;&#x5148;&#xFF0C;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B0;&#x7684;URL&#x8DEF;&#x5F84;&#xFF1A;</p>
<p><strong>myproject/urls.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin
<span class="hljs-keyword">from</span> django.contrib.auth <span class="hljs-keyword">import</span> views <span class="hljs-keyword">as</span> auth_views

<span class="hljs-keyword">from</span> accounts <span class="hljs-keyword">import</span> views <span class="hljs-keyword">as</span> accounts_views
<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^signup/$&apos;</span>, accounts_views.signup, name=<span class="hljs-string">&apos;signup&apos;</span>),
    url(<span class="hljs-string">r&apos;^login/$&apos;</span>, auth_views.LoginView.as_view(template_name=<span class="hljs-string">&apos;login.html&apos;</span>), name=<span class="hljs-string">&apos;login&apos;</span>),
    url(<span class="hljs-string">r&apos;^logout/$&apos;</span>, auth_views.LogoutView.as_view(), name=<span class="hljs-string">&apos;logout&apos;</span>),
    url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.board_topics, name=<span class="hljs-string">&apos;board_topics&apos;</span>),
    url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/new/$&apos;</span>, views.new_topic, name=<span class="hljs-string">&apos;new_topic&apos;</span>),
    url(<span class="hljs-string">r&apos;^admin/&apos;</span>, admin.site.urls),
]
</code></pre>
<p>&#x5728;<code>as_view()</code>&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x4E00;&#x4E9B;&#x989D;&#x5916;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4EE5;&#x8986;&#x76D6;&#x9ED8;&#x8BA4;&#x503C;&#x3002;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x8BA9;<strong>LoginView</strong> &#x4F7F;&#x7528;<strong>login.html</strong>&#x6A21;&#x677F;&#x3002;</p>
<p>&#x7F16;&#x8F91;<strong>settings.py</strong>&#x7136;&#x540E;&#x6DFB;&#x52A0;</p>
<p><strong>myproject/settings.py</strong></p>
<pre><code>LOGIN_REDIRECT_URL = &apos;home&apos;
</code></pre><p>&#x8FD9;&#x4E2A;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x544A;&#x8BC9;Django&#x5728;&#x6210;&#x529F;&#x767B;&#x5F55;&#x540E;&#x5C06;&#x7528;&#x6237;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x54EA;&#x91CC;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x5C06;&#x767B;&#x5F55;URL&#x6DFB;&#x52A0;&#x5230; <strong>base.html</strong>&#x6A21;&#x677F;&#x4E2D;&#xFF1A;</p>
<p><strong>templates/base.html</strong></p>
<pre><code>&lt;a href=&quot;{% url &apos;login&apos; %}&quot; class=&quot;btn btn-outline-secondary&quot;&gt;Log in&lt;/a&gt;
</code></pre><p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x4F3C;&#x4E8E;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x7684;&#x6A21;&#x677F;&#x3002;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <strong>login.html</strong> &#x7684;&#x65B0;&#x6587;&#x4EF6;&#xFF1A;</p>
<p><strong>templates/login.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% load static %}

{% block stylesheet %}
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% static &apos;css/accounts.css&apos; %}&quot;</span>&gt;</span>
{% endblock %}

{% block body %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-center logo my-4&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Django Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-4 col-md-6 col-sm-8&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
              {% csrf_token %}
              {% include &apos;includes/form.html&apos; %}
              <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary btn-block&quot;</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-footer text-muted text-center&quot;</span>&gt;</span>
            New to Django Boards? <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;signup&apos; %}&quot;</span>&gt;</span>Sign up<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-center py-2&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">small</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-muted&quot;</span>&gt;</span>Forgot your password?<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/login.jpg" alt="Login"></p>
<p>&#x6211;&#x4EEC;&#x770B;&#x5230;HTML&#x6A21;&#x677F;&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x91CD;&#x590D;&#x4E86;&#xFF0C;&#x73B0;&#x5728;&#x6765;&#x91CD;&#x6784;&#x4E00;&#x4E0B;&#x5B83;&#x3002;</p>
<p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<strong>base_accounts.html</strong>&#x7684;&#x65B0;&#x6A21;&#x677F;&#xFF1A;</p>
<p><strong>templates/base_accounts.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% load static %}

{% block stylesheet %}
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% static &apos;css/accounts.css&apos; %}&quot;</span>&gt;</span>
{% endblock %}

{% block body %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-center logo my-4&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Django Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    {% block content %}
    {% endblock %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x73B0;&#x5728;&#x5728;<strong>signup.html</strong>&#x548C;<strong>login.html</strong>&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</p>
<p><strong>templates/login.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base_accounts.html&apos; %}

{% block title %}Log in to Django Boards{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-4 col-md-6 col-sm-8&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
            {% csrf_token %}
            {% include &apos;includes/form.html&apos; %}
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary btn-block&quot;</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-footer text-muted text-center&quot;</span>&gt;</span>
          New to Django Boards? <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;signup&apos; %}&quot;</span>&gt;</span>Sign up<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-center py-2&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">small</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-muted&quot;</span>&gt;</span>Forgot your password?<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x6211;&#x4EEC;&#x6709;&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x56E0;&#x6B64;&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x5C06;&#x5176;&#x6682;&#x65F6;&#x4FDD;&#x7559;&#x4E3A;<code>#</code>&#x3002;</p>
<p><strong>templates/signup.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base_accounts.html&apos; %}

{% block title %}Sign up to Django Boards{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-8 col-md-10 col-sm-12&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Sign up<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
            {% csrf_token %}
            {% include &apos;includes/form.html&apos; %}
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary btn-block&quot;</span>&gt;</span>Create an account<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-footer text-muted text-center&quot;</span>&gt;</span>
          Already have an account? <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;login&apos; %}&quot;</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86;&#x767B;&#x5F55;&#x94FE;&#x63A5;&#xFF1A; <code>&lt;a href=&quot;{% url &apos;login&apos; %}&quot;&gt;Log in&lt;/a&gt;</code>.</p>
<h5 id="&#x65E0;&#x767B;&#x5F55;&#x4FE1;&#x606F;&#x9519;&#x8BEF;">&#x65E0;&#x767B;&#x5F55;&#x4FE1;&#x606F;&#x9519;&#x8BEF;</h5>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x63D0;&#x4EA4;&#x7A7A;&#x767D;&#x7684;&#x767B;&#x5F55;&#x4FE1;&#x606F;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E9B;&#x53CB;&#x597D;&#x7684;&#x9519;&#x8BEF;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#xFF1A;</p>
<p><img src="statics/login-1.jpg" alt="Login"></p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x7528;&#x6237;&#x540D;&#x6216;&#x4E00;&#x4E2A;&#x65E0;&#x6548;&#x7684;&#x5BC6;&#x7801;&#xFF0C;&#x73B0;&#x5728;&#x5C31;&#x4F1A;&#x53D1;&#x751F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;</p>
<p><img src="statics/login-2.jpg" alt="Login"></p>
<p>&#x6709;&#x70B9;&#x8BEF;&#x5BFC;&#xFF0C;&#x8FD9;&#x4E2A;&#x533A;&#x57DF;&#x662F;&#x7EFF;&#x8272;&#x7684;&#xFF0C;&#x8868;&#x660E;&#x5B83;&#x4EEC;&#x662F;&#x826F;&#x597D;&#x8FD0;&#x884C;&#x7684;&#xFF0C;&#x6B64;&#x5916;&#xFF0C;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x989D;&#x5916;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x8868;&#x5355;&#x6709;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7C7B;&#x578B;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x53EB;&#x505A; <strong>non-field errors</strong>&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x7EC4;&#x4E0E;&#x7279;&#x5B9A;&#x5B57;&#x6BB5;&#x65E0;&#x5173;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x91CD;&#x6784;<strong>form.html</strong>&#x90E8;&#x5206;&#x6A21;&#x677F;&#x4EE5;&#x663E;&#x793A;&#x8FD9;&#x4E9B;&#x9519;&#x8BEF;&#xFF1A;</p>
<p><strong>templates/includes/form.html</strong></p>
<pre><code class="lang-html">{% load widget_tweaks %}

{% if form.non_field_errors %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;alert alert-danger&quot;</span> <span class="hljs-attr">role</span>=<span class="hljs-string">&quot;alert&quot;</span>&gt;</span>
    {% for error in form.non_field_errors %}
      <span class="hljs-tag">&lt;<span class="hljs-name">p{%</span> <span class="hljs-attr">if</span> <span class="hljs-attr">forloop.last</span> %} <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mb-0&quot;</span><span class="hljs-string">{%</span> <span class="hljs-attr">endif</span> %}&gt;</span>{{ error }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    {% endfor %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endif %}

{% for field in form %}
  <span class="hljs-comment">&lt;!-- code suppressed --&gt;</span>
{% endfor %}
</code></pre>
<p><code>{% if forloop.last %}</code>&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x5C0F;&#x4E8B;&#x60C5;&#xFF0C;&#x56E0;&#x4E3A;<code>p</code>&#x6807;&#x7B7E;&#x6709;&#x4E00;&#x4E2A;&#x7A7A;&#x767D;&#x7684;<code>margin-bottom</code>.&#x4E00;&#x4E2A;&#x8868;&#x5355;&#x53EF;&#x80FD;&#x6709;&#x51E0;&#x4E2A;<strong>non-field error</strong>&#xFF0C;&#x6211;&#x4EEC;&#x5448;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x9519;&#x8BEF;&#x7684;<code>p</code>&#x6807;&#x7B7E;&#x3002;&#x7136;&#x540E;&#x6211;&#x8981;&#x68C0;&#x67E5;&#x5B83;&#x662F;&#x5426;&#x662F;&#x6700;&#x540E;&#x4E00;&#x6B21;&#x6E32;&#x67D3;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x5982;&#x679C;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A; Bootstrap 4 CSS&#x7C7B; <code>mb-0</code> &#xFF0C;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x4EE3;&#x8868;&#x4E86;&#x201C;margin bottom = 0&#x201D;&#xFF08;&#x5E95;&#x90E8;&#x8FB9;&#x7F18;&#x4E3A;0&#xFF09;&#x3002;&#x8FD9;&#x6837;&#x7684;&#x8BDD;&#x8B66;&#x544A;&#x770B;&#x8D77;&#x6765;&#x5C31;&#x4E0D;&#x90A3;&#x4E48;&#x5947;&#x602A;&#x4E86;&#x5E76;&#x4E14;&#x591A;&#x4E86;&#x4E00;&#x4E9B;&#x989D;&#x5916;&#x7684;&#x7A7A;&#x95F4;&#x3002;&#x8FD9;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x5C0F;&#x7684;&#x7EC6;&#x8282;&#x3002;&#x6211;&#x8FD9;&#x4E48;&#x505A;&#x7684;&#x539F;&#x56E0;&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x4FDD;&#x6301;&#x95F4;&#x8DDD;&#x7684;&#x4E00;&#x81F4;&#x6027;&#x3002;</p>
<p><img src="statics/login-3.jpg" alt="Login"></p>
<p>&#x5C3D;&#x7BA1;&#x5982;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x4ECD;&#x7136;&#x9700;&#x8981;&#x5904;&#x7406;&#x5BC6;&#x7801;&#x5B57;&#x6BB5;&#x3002;&#x95EE;&#x9898;&#x5728;&#x4E8E;&#xFF0C;Django&#x4ECE;&#x4E0D;&#x5C06;&#x5BC6;&#x7801;&#x5B57;&#x6BB5;&#x7684;&#x6570;&#x636E;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E0D;&#x8981;&#x8BD5;&#x56FE;&#x505A;&#x4E00;&#x6B21;&#x81EA;&#x4F5C;&#x806A;&#x660E;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5FFD;&#x7565;<code>is-valid</code>&#x548C;<code>is-invalid</code> &#x7684;CSS&#x7C7B;&#x3002;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x8868;&#x5355;&#x6A21;&#x677F;&#x770B;&#x8D77;&#x6765;&#x5341;&#x5206;&#x7684;&#x590D;&#x6742;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x4E00;&#x4E9B;&#x4EE3;&#x7801;&#x79FB;&#x52A8;&#x5230;&#x6A21;&#x677F;&#x6807;&#x8BB0;&#x4E2D;&#x53BB;&#x3002;</p>
<h3 id="&#x521B;&#x5EFA;&#x81EA;&#x5B9A;&#x4E49;&#x6A21;&#x677F;&#x6807;&#x7B7E;">&#x521B;&#x5EFA;&#x81EA;&#x5B9A;&#x4E49;&#x6A21;&#x677F;&#x6807;&#x7B7E;</h3>
<p>&#x5728;<strong>boards</strong>&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<strong>templatetags</strong>&#x7684;&#x65B0;&#x6587;&#x4EF6;&#x5939;&#x3002;&#x7136;&#x540E;&#x5728;&#x8BE5;&#x6587;&#x4EF6;&#x5939;&#x5185;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x540D;&#x4E3A; <strong><strong>init</strong>.py</strong> &#x548C; <strong>form_tags.py</strong>&#x7684;&#x7A7A;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x6587;&#x4EF6;&#x7ED3;&#x6784;&#x5E94;&#x8BE5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-shell">myproject/
 |-- myproject/
 |    |-- accounts/
 |    |-- boards/
 |    |    |-- migrations/
 |    |    |-- templatetags/        &lt;-- here
 |    |    |    |-- __init__.py
 |    |    |    +-- form_tags.py
 |    |    |-- __init__.py
 |    |    |-- admin.py
 |    |    |-- apps.py
 |    |    |-- models.py
 |    |    |-- tests.py
 |    |    +-- views.py
 |    |-- myproject/
 |    |-- static/
 |    |-- templates/
 |    |-- db.sqlite3
 |    +-- manage.py
 +-- venv/
</code></pre>
<p>&#x5728; <strong>form_tags.py</strong>&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x6A21;&#x677F;&#x6807;&#x7B7E;&#xFF1A;</p>
<p><strong>boards/templatetags/form_tags.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> template

register = template.Library()

<span class="hljs-meta">@register.filter</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">field_type</span><span class="hljs-params">(bound_field)</span>:</span>
    <span class="hljs-keyword">return</span> bound_field.field.widget.__class__.__name__

<span class="hljs-meta">@register.filter</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">input_class</span><span class="hljs-params">(bound_field)</span>:</span>
    css_class = <span class="hljs-string">&apos;&apos;</span>
    <span class="hljs-keyword">if</span> bound_field.form.is_bound:
        <span class="hljs-keyword">if</span> bound_field.errors:
            css_class = <span class="hljs-string">&apos;is-invalid&apos;</span>
        <span class="hljs-keyword">elif</span> field_type(bound_field) != <span class="hljs-string">&apos;PasswordInput&apos;</span>:
            css_class = <span class="hljs-string">&apos;is-valid&apos;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;form-control {}&apos;</span>.format(css_class)
</code></pre>
<p>&#x8FD9;&#x4E9B;&#x662F;&#x6A21;&#x677F;&#x8FC7;&#x6EE4;&#x5668;&#xFF0C;&#x4ED6;&#x4EEC;&#x7684;&#x5DE5;&#x4F5C;&#x65B9;&#x5F0F;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5B83;&#x52A0;&#x8F7D;&#x5230;&#x6A21;&#x677F;&#x4E2D;&#xFF0C;&#x5C31;&#x50CF;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <strong>widget_tweaks</strong> &#x6216;<strong>static</strong> &#x6A21;&#x677F;&#x6807;&#x7B7E;&#x4E00;&#x6837;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x540E;&#xFF0C;&#x4F60;&#x5C06;&#x4E0D;&#x5F97;&#x4E0D;&#x624B;&#x52A8;&#x505C;&#x6B62;&#x5F00;&#x53D1;&#x670D;&#x52A1;&#x5668;&#x5E76;&#x91CD;&#x542F;&#x5B83;&#xFF0C;&#x4EE5;&#x4FBF;Django&#x53EF;&#x4EE5;&#x8BC6;&#x522B;&#x65B0;&#x7684;&#x6A21;&#x677F;&#x6807;&#x7B7E;&#x3002;</p>
<pre><code>{% load form_tags %}
</code></pre><p>&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x4E86;&#x3002;</p>
<pre><code>{{ form.username|field_type }}
</code></pre><p>&#x8FD4;&#x56DE;&#xFF1A;</p>
<pre><code>&apos;TextInput&apos;
</code></pre><p>&#x6216;&#x8005;&#x5728; <strong>input_class</strong>&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">{{ form.username|input_class }}

&lt;!-- if the form is not bound, it will simply return: --&gt;
&apos;form-control &apos;

&lt;!-- if the form is bound and valid: --&gt;
&apos;form-control is-valid&apos;

&lt;!-- if the form is bound and invalid: --&gt;
&apos;form-control is-invalid&apos;
</code></pre>
<p>&#x73B0;&#x5728;&#x66F4;&#x65B0; <strong>form.html</strong>&#x4EE5;&#x4F7F;&#x7528;&#x65B0;&#x7684;&#x6A21;&#x677F;&#x6807;&#x7B7E;&#xFF1A;</p>
<p><strong>templates/includes/form.html</strong></p>
<pre><code class="lang-html">{% load form_tags widget_tweaks %}

{% if form.non_field_errors %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;alert alert-danger&quot;</span> <span class="hljs-attr">role</span>=<span class="hljs-string">&quot;alert&quot;</span>&gt;</span>
    {% for error in form.non_field_errors %}
      <span class="hljs-tag">&lt;<span class="hljs-name">p{%</span> <span class="hljs-attr">if</span> <span class="hljs-attr">forloop.last</span> %} <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mb-0&quot;</span><span class="hljs-string">{%</span> <span class="hljs-attr">endif</span> %}&gt;</span>{{ error }}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    {% endfor %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endif %}

{% for field in form %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-group&quot;</span>&gt;</span>
    {{ field.label_tag }}
    {% render_field field class=field|input_class %}
    {% for error in field.errors %}
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;invalid-feedback&quot;</span>&gt;</span>
        {{ error }}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    {% endfor %}
    {% if field.help_text %}
      <span class="hljs-tag">&lt;<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-text text-muted&quot;</span>&gt;</span>
        {{ field.help_text|safe }}
      <span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span>
    {% endif %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endfor %}
</code></pre>
<p>&#x8FD9;&#x6837;&#x7684;&#x8BDD;&#x5C31;&#x597D;&#x591A;&#x4E86;&#x662F;&#x5427;&#xFF1F;&#x8FD9;&#x6837;&#x505A;&#x964D;&#x4F4E;&#x4E86;&#x6A21;&#x677F;&#x7684;&#x590D;&#x6742;&#x6027;&#xFF0C;&#x5B83;&#x73B0;&#x5728;&#x770B;&#x8D77;&#x6765;&#x66F4;&#x52A0;&#x6574;&#x6D01;&#x3002;&#x5E76;&#x4E14;&#x5B83;&#x8FD8;&#x89E3;&#x51B3;&#x4E86;&#x5BC6;&#x7801;&#x5B57;&#x6BB5;&#x663E;&#x793A;&#x7EFF;&#x8272;&#x8FB9;&#x6846;&#x7684;&#x95EE;&#x9898;&#xFF1A;</p>
<p><img src="statics/login-4.jpg" alt="Login"></p>
<h3 id="&#x6D4B;&#x8BD5;&#x6A21;&#x677F;&#x6807;&#x7B7E;">&#x6D4B;&#x8BD5;&#x6A21;&#x677F;&#x6807;&#x7B7E;</h3>
<p>&#x9996;&#x5148;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x7A0D;&#x5FAE;&#x7EC4;&#x7EC7;&#x4E00;&#x4E0B;<strong>boards</strong>&#x7684;&#x6D4B;&#x8BD5;&#x3002;&#x5C31;&#x50CF;&#x6211;&#x4EEC;&#x5BF9;account app &#x6240;&#x505A;&#x7684;&#x90A3;&#x6837;&#x3002;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6587;&#x4EF6;&#x5939;&#x540D;&#x4E3A;<strong>tests</strong>&#xFF0C;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;<strong>init.py</strong>&#xFF0C;&#x590D;&#x5236;<strong>test.py</strong>&#x5E76;&#x4E14;&#x5C06;&#x5176;&#x91CD;&#x547D;&#x540D;&#x4E3A;<strong>test_views.py</strong>&#x3002;</p>
<p>&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <strong>test_templatetags.py</strong>&#x7684;&#x65B0;&#x7A7A;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-shell">myproject/
 |-- myproject/
 |    |-- accounts/
 |    |-- boards/
 |    |    |-- migrations/
 |    |    |-- templatetags/
 |    |    |-- tests/
 |    |    |    |-- __init__.py
 |    |    |    |-- test_templatetags.py  &lt;-- new file, empty for now
 |    |    |    +-- test_views.py  &lt;-- our old file with all the tests
 |    |    |-- __init__.py
 |    |    |-- admin.py
 |    |    |-- apps.py
 |    |    |-- models.py
 |    |    +-- views.py
 |    |-- myproject/
 |    |-- static/
 |    |-- templates/
 |    |-- db.sqlite3
 |    +-- manage.py
 +-- venv/
</code></pre>
<p>&#x4FEE;&#x590D;<strong>test_views.py</strong>&#x7684;&#x5BFC;&#x5165;&#x95EE;&#x9898;&#xFF1A;</p>
<p><strong>boards/tests/test_views.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> ..views <span class="hljs-keyword">import</span> home, board_topics, new_topic
<span class="hljs-keyword">from</span> ..models <span class="hljs-keyword">import</span> Board, Topic, Post
<span class="hljs-keyword">from</span> ..forms <span class="hljs-keyword">import</span> NewTopicForm
</code></pre>
<p>&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#x6765;&#x786E;&#x4FDD;&#x4E00;&#x5207;&#x90FD;&#x6B63;&#x5E38;&#x3002;</p>
<p><strong>boards/tests/test_templatetags.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase
<span class="hljs-keyword">from</span> ..templatetags.form_tags <span class="hljs-keyword">import</span> field_type, input_class

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleForm</span><span class="hljs-params">(forms.Form)</span>:</span>
    name = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput())
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        fields = (<span class="hljs-string">&apos;name&apos;</span>, <span class="hljs-string">&apos;password&apos;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FieldTypeTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_field_widget_type</span><span class="hljs-params">(self)</span>:</span>
        form = ExampleForm()
        self.assertEquals(<span class="hljs-string">&apos;TextInput&apos;</span>, field_type(form[<span class="hljs-string">&apos;name&apos;</span>]))
        self.assertEquals(<span class="hljs-string">&apos;PasswordInput&apos;</span>, field_type(form[<span class="hljs-string">&apos;password&apos;</span>]))

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InputClassTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_unbound_field_initial_state</span><span class="hljs-params">(self)</span>:</span>
        form = ExampleForm()  <span class="hljs-comment"># unbound form</span>
        self.assertEquals(<span class="hljs-string">&apos;form-control &apos;</span>, input_class(form[<span class="hljs-string">&apos;name&apos;</span>]))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_valid_bound_field</span><span class="hljs-params">(self)</span>:</span>
        form = ExampleForm({<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;john&apos;</span>, <span class="hljs-string">&apos;password&apos;</span>: <span class="hljs-string">&apos;123&apos;</span>})  <span class="hljs-comment"># bound form (field + data)</span>
        self.assertEquals(<span class="hljs-string">&apos;form-control is-valid&apos;</span>, input_class(form[<span class="hljs-string">&apos;name&apos;</span>]))
        self.assertEquals(<span class="hljs-string">&apos;form-control &apos;</span>, input_class(form[<span class="hljs-string">&apos;password&apos;</span>]))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_invalid_bound_field</span><span class="hljs-params">(self)</span>:</span>
        form = ExampleForm({<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;&apos;</span>, <span class="hljs-string">&apos;password&apos;</span>: <span class="hljs-string">&apos;123&apos;</span>})  <span class="hljs-comment"># bound form (field + data)</span>
        self.assertEquals(<span class="hljs-string">&apos;form-control is-invalid&apos;</span>, input_class(form[<span class="hljs-string">&apos;name&apos;</span>]))
</code></pre>
<p>&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x6D4B;&#x8BD5;&#x7684;&#x8868;&#x5355;&#x7C7B;&#xFF0C;&#x7136;&#x540E;&#x6DFB;&#x52A0;&#x4E86;&#x8986;&#x76D6;&#x4E24;&#x4E2A;&#x6A21;&#x677F;&#x6807;&#x8BB0;&#x4E2D;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x7684;&#x573A;&#x666F;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
................................
----------------------------------------------------------------------
Ran 32 tests in 0.846s

OK
Destroying test database for alias &apos;default&apos;...
</code></pre>
<h3 id="&#x5BC6;&#x7801;&#x91CD;&#x7F6E;">&#x5BC6;&#x7801;&#x91CD;&#x7F6E;</h3>
<p>&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x8FC7;&#x7A0B;&#x4E2D;&#x6D89;&#x53CA;&#x4E00;&#x4E9B;&#x4E0D;&#x53CB;&#x597D;&#x7684; URL &#x6A21;&#x5F0F;&#x3002;&#x4F46;&#x6B63;&#x5982;&#x6211;&#x4EEC;&#x5728;&#x524D;&#x9762;&#x7684;&#x6559;&#x7A0B;&#x4E2D;&#x8BA8;&#x8BBA;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x6210;&#x4E3A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4E13;&#x5BB6;&#x3002;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4E86;&#x89E3;&#x5E38;&#x89C1;&#x95EE;&#x9898;&#x548C;&#x5B83;&#x4EEC;&#x7684;&#x89E3;&#x51B3;&#x529E;&#x6CD5;&#x3002;</p>
<p>&#x5728;&#x6211;&#x4EEC;&#x5F00;&#x59CB;&#x4E4B;&#x524D;&#x53E6;&#x4E00;&#x4EF6;&#x91CD;&#x8981;&#x7684;&#x4E8B;&#x60C5;&#x662F;&#xFF0C;&#x5BF9;&#x4E8E;&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x8FC7;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x53D1;&#x9001;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x3002;&#x4E00;&#x5F00;&#x59CB;&#x6709;&#x70B9;&#x590D;&#x6742;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5916;&#x90E8;&#x670D;&#x52A1;&#x3002;&#x76EE;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x914D;&#x7F6E;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#x4F7F;&#x7528;&#x7684;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x670D;&#x52A1;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x5728;&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;Django&#x7684;&#x8C03;&#x8BD5;&#x5DE5;&#x5177;&#x68C0;&#x67E5;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x662F;&#x5426;&#x6B63;&#x786E;&#x53D1;&#x9001;&#x3002;</p>
<p><img src="statics/4-31.jpg" alt=""></p>
<h3 id="&#x63A7;&#x5236;&#x53F0;&#x6536;&#x53D1;email">&#x63A7;&#x5236;&#x53F0;&#x6536;&#x53D1;Email</h3>
<p>&#x8FD9;&#x4E2A;&#x4E3B;&#x610F;&#x6765;&#x81EA;&#x4E8E;&#x9879;&#x76EE;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x53D1;&#x9001;&#x771F;&#x5B9E;&#x7684;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x8BB0;&#x5F55;&#x5B83;&#x4EEC;&#x3002;&#x6211;&#x4EEC;&#x6709;&#x4E24;&#x79CD;&#x9009;&#x62E9;&#xFF1A;&#x5C06;&#x6240;&#x6709;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x5199;&#x5165;&#x6587;&#x672C;&#x6587;&#x4EF6;&#x6216;&#x4EC5;&#x5C06;&#x5176;&#x663E;&#x793A;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x4E2D;&#x3002;&#x6211;&#x53D1;&#x73B0;&#x7B2C;&#x4E8C;&#x4E2A;&#x65B9;&#x5F0F;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5728;&#x4F7F;&#x7528;&#x63A7;&#x5236;&#x53F0;&#x6765;&#x8FD0;&#x884C;&#x5F00;&#x53D1;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5E76;&#x4E14;&#x8BBE;&#x7F6E;&#x66F4;&#x5BB9;&#x6613;&#x4E00;&#x4E9B;&#x3002;</p>
<p>&#x7F16;&#x8F91; <strong>settings.py</strong>&#x6A21;&#x5757;&#x5E76;&#x5C06;<code>EMAIL_BACKEND</code>&#x53D8;&#x91CF;&#x6DFB;&#x52A0;&#x5230;&#x6587;&#x4EF6;&#x7684;&#x672B;&#x5C3E;&#x3002;</p>
<p><strong>myproject/settings.py</strong></p>
<pre><code class="lang-python">EMAIL_BACKEND = <span class="hljs-string">&apos;django.core.mail.backends.console.EmailBackend&apos;</span>
</code></pre>
<h3 id="&#x914D;&#x7F6E;&#x8DEF;&#x7531;">&#x914D;&#x7F6E;&#x8DEF;&#x7531;</h3>
<p>&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x8FC7;&#x7A0B;&#x9700;&#x8981;&#x56DB;&#x4E2A;&#x89C6;&#x56FE;&#xFF1A;</p>
<ul>
<li>&#x5E26;&#x6709;&#x8868;&#x5355;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x7528;&#x4E8E;&#x542F;&#x52A8;&#x91CD;&#x7F6E;&#x8FC7;&#x7A0B;&#xFF1B;</li>
<li>&#x4E00;&#x4E2A;&#x6210;&#x529F;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x8868;&#x793A;&#x8BE5;&#x8FC7;&#x7A0B;&#x5DF2;&#x542F;&#x52A8;&#xFF0C;&#x6307;&#x793A;&#x7528;&#x6237;&#x68C0;&#x67E5;&#x5176;&#x90AE;&#x4EF6;&#x6587;&#x4EF6;&#x5939;&#x7B49;&#xFF1B;</li>
<li>&#x68C0;&#x67E5;&#x901A;&#x8FC7;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x53D1;&#x9001;token&#x7684;&#x9875;&#x9762;</li>
<li>&#x4E00;&#x4E2A;&#x544A;&#x8BC9;&#x7528;&#x6237;&#x91CD;&#x7F6E;&#x662F;&#x5426;&#x6210;&#x529F;&#x7684;&#x9875;&#x9762;</li>
</ul>
<p>&#x8FD9;&#x4E9B;&#x89C6;&#x56FE;&#x662F;&#x5185;&#x7F6E;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x9700;&#x8981;&#x6267;&#x884C;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x6240;&#x9700;&#x8981;&#x505A;&#x7684;&#x5C31;&#x662F;&#x5C06;&#x8DEF;&#x5F84;&#x6DFB;&#x52A0;&#x5230; <strong>urls.py</strong>&#x5E76;&#x4E14;&#x521B;&#x5EFA;&#x6A21;&#x677F;&#x3002;</p>
<p><strong>myproject/urls.py</strong> <a href="https://gist.github.com/vitorfs/117e300e00d5685f7186e09260f82736#file-urls-py-L14" target="_blank">(&#x5B8C;&#x6574;&#x4EE3;&#x7801;)</a></p>
<pre><code class="lang-python">url(<span class="hljs-string">r&apos;^reset/$&apos;</span>,
    auth_views.PasswordResetView.as_view(
        template_name=<span class="hljs-string">&apos;password_reset.html&apos;</span>,
        email_template_name=<span class="hljs-string">&apos;password_reset_email.html&apos;</span>,
        subject_template_name=<span class="hljs-string">&apos;password_reset_subject.txt&apos;</span>
    ),
    name=<span class="hljs-string">&apos;password_reset&apos;</span>),
url(<span class="hljs-string">r&apos;^reset/done/$&apos;</span>,
    auth_views.PasswordResetDoneView.as_view(template_name=<span class="hljs-string">&apos;password_reset_done.html&apos;</span>),
    name=<span class="hljs-string">&apos;password_reset_done&apos;</span>),
url(<span class="hljs-string">r&apos;^reset/(?P&lt;uidb64&gt;[0-9A-Za-z_\-]+)/(?P&lt;token&gt;[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$&apos;</span>,
    auth_views.PasswordResetConfirmView.as_view(template_name=<span class="hljs-string">&apos;password_reset_confirm.html&apos;</span>),
    name=<span class="hljs-string">&apos;password_reset_confirm&apos;</span>),
url(<span class="hljs-string">r&apos;^reset/complete/$&apos;</span>,
    auth_views.PasswordResetCompleteView.as_view(template_name=<span class="hljs-string">&apos;password_reset_complete.html&apos;</span>),
    name=<span class="hljs-string">&apos;password_reset_complete&apos;</span>),
]
</code></pre>
<p>&#x5728;&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x89C6;&#x56FE;&#x4E2D;&#xFF0C;<code>template_name</code>&#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;&#x4F46;&#x6211;&#x8BA4;&#x4E3A;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x5B83;&#x662F;&#x4E2A;&#x597D;&#x4E3B;&#x610F;&#xFF0C;&#x56E0;&#x6B64;&#x89C6;&#x56FE;&#x548C;&#x6A21;&#x677F;&#x4E4B;&#x95F4;&#x7684;&#x94FE;&#x63A5;&#x6BD4;&#x4EC5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x503C;&#x66F4;&#x52A0;&#x660E;&#x663E;&#x3002;</p>
<p>&#x5728; <strong>templates</strong>&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#xFF0C;&#x65B0;&#x589E;&#x5982;&#x4E0B;&#x6A21;&#x677F;&#x6587;&#x4EF6;</p>
<ul>
<li><strong>password_reset.html</strong></li>
<li><strong>password_reset_email.html</strong>:&#x8FD9;&#x4E2A;&#x6A21;&#x677F;&#x662F;&#x53D1;&#x9001;&#x7ED9;&#x7528;&#x6237;&#x7684;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x6B63;&#x6587;</li>
<li><strong>password_reset_subject.txt</strong>:&#x8FD9;&#x4E2A;&#x6A21;&#x677F;&#x662F;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x7684;&#x4E3B;&#x9898;&#x884C;&#xFF0C;&#x5B83;&#x5E94;&#x8BE5;&#x662F;&#x5355;&#x884C;&#x6587;&#x4EF6;</li>
<li><strong>password_reset_done.html</strong></li>
<li><strong>password_reset_confirm.html</strong></li>
<li><strong>password_reset_complete.html</strong></li>
</ul>
<p>&#x5728;&#x6211;&#x4EEC;&#x5F00;&#x59CB;&#x5B9E;&#x73B0;&#x6A21;&#x677F;&#x4E4B;&#x524D;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x51C6;&#x5907;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x57FA;&#x672C;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E9B;&#x89C6;&#x56FE;&#x548C;&#x8868;&#x5355;&#x5DF2;&#x7ECF;&#x5728;Django&#x4EE3;&#x7801;&#x4E2D;&#x8FDB;&#x884C;&#x4E86;&#x6D4B;&#x8BD5;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x53EA;&#x6D4B;&#x8BD5;&#x6211;&#x4EEC;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x7EC6;&#x8282;&#x3002;</p>
<p>&#x5728;<strong>accounts/tests</strong> &#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <strong>test_view_password_reset.py</strong> &#x7684;&#x65B0;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x3002;</p>
<h3 id="&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x89C6;&#x56FE;">&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x89C6;&#x56FE;</h3>
<p><strong>templates/password_reset.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base_accounts.html&apos; %}

{% block title %}Reset your password{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-4 col-md-6 col-sm-8&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Reset your password<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Enter your email address and we will send you a link to reset your password.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
            {% csrf_token %}
            {% include &apos;includes/form.html&apos; %}
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary btn-block&quot;</span>&gt;</span>Send password reset email<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/password_reset.jpg" alt="Password Reset"></p>
<p><strong>accounts/tests/test_view_password_reset.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth <span class="hljs-keyword">import</span> views <span class="hljs-keyword">as</span> auth_views
<span class="hljs-keyword">from</span> django.contrib.auth.forms <span class="hljs-keyword">import</span> PasswordResetForm
<span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User
<span class="hljs-keyword">from</span> django.core <span class="hljs-keyword">import</span> mail
<span class="hljs-keyword">from</span> django.core.urlresolvers <span class="hljs-keyword">import</span> reverse
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> resolve
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PasswordResetTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;password_reset&apos;</span>)
        self.response = self.client.get(url)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_status_code</span><span class="hljs-params">(self)</span>:</span>
        self.assertEquals(self.response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_view_function</span><span class="hljs-params">(self)</span>:</span>
        view = resolve(<span class="hljs-string">&apos;/reset/&apos;</span>)
        self.assertEquals(view.func.view_class, auth_views.PasswordResetView)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_csrf</span><span class="hljs-params">(self)</span>:</span>
        self.assertContains(self.response, <span class="hljs-string">&apos;csrfmiddlewaretoken&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_contains_form</span><span class="hljs-params">(self)</span>:</span>
        form = self.response.context.get(<span class="hljs-string">&apos;form&apos;</span>)
        self.assertIsInstance(form, PasswordResetForm)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_form_inputs</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        The view must contain two inputs: csrf and email
        &apos;&apos;&apos;</span>
        self.assertContains(self.response, <span class="hljs-string">&apos;&lt;input&apos;</span>, <span class="hljs-number">2</span>)
        self.assertContains(self.response, <span class="hljs-string">&apos;type=&quot;email&quot;&apos;</span>, <span class="hljs-number">1</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SuccessfulPasswordResetTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        email = <span class="hljs-string">&apos;john@doe.com&apos;</span>
        User.objects.create_user(username=<span class="hljs-string">&apos;john&apos;</span>, email=email, password=<span class="hljs-string">&apos;123abcdef&apos;</span>)
        url = reverse(<span class="hljs-string">&apos;password_reset&apos;</span>)
        self.response = self.client.post(url, {<span class="hljs-string">&apos;email&apos;</span>: email})

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_redirection</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        A valid form submission should redirect the user to `password_reset_done` view
        &apos;&apos;&apos;</span>
        url = reverse(<span class="hljs-string">&apos;password_reset_done&apos;</span>)
        self.assertRedirects(self.response, url)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_send_password_reset_email</span><span class="hljs-params">(self)</span>:</span>
        self.assertEqual(<span class="hljs-number">1</span>, len(mail.outbox))


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidPasswordResetTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;password_reset&apos;</span>)
        self.response = self.client.post(url, {<span class="hljs-string">&apos;email&apos;</span>: <span class="hljs-string">&apos;donotexist@email.com&apos;</span>})

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_redirection</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        Even invalid emails in the database should
        redirect the user to `password_reset_done` view
        &apos;&apos;&apos;</span>
        url = reverse(<span class="hljs-string">&apos;password_reset_done&apos;</span>)
        self.assertRedirects(self.response, url)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_no_reset_email_sent</span><span class="hljs-params">(self)</span>:</span>
        self.assertEqual(<span class="hljs-number">0</span>, len(mail.outbox))
</code></pre>
<p><strong>templates/password_reset_subject.txt</strong></p>
<pre><code>[Django Boards] Please reset your password
</code></pre><p><strong>templates/password_reset_email.html</strong></p>
<pre><code class="lang-text">Hi there,

Someone asked for a password reset for the email address {{ email }}.
Follow the link below:
{{ protocol }}://{{ domain }}{% url &apos;password_reset_confirm&apos; uidb64=uid token=token %}

In case you forgot your Django Boards username: {{ user.username }}

If clicking the link above doesn&apos;t work, please copy and paste the URL
in a new browser window instead.

If you&apos;ve received this mail in error, it&apos;s likely that another user entered
your email address by mistake while trying to reset a password. If you didn&apos;t
initiate the request, you don&apos;t need to take any further action and can safely
disregard this email.

Thanks,

The Django Boards Team
</code></pre>
<p><img src="statics/password_reset_email.png" alt="Password Reset Email"></p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x6765;&#x6D4B;&#x8BD5;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x3002;&#x5728;<strong>accounts/tests</strong> &#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<strong>test_mail_password_reset.py</strong>&#x7684;&#x65B0;&#x6587;&#x4EF6;&#xFF1A;</p>
<p><strong>accounts/tests/test_mail_password_reset.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.core <span class="hljs-keyword">import</span> mail
<span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> reverse
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PasswordResetMailTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        User.objects.create_user(username=<span class="hljs-string">&apos;john&apos;</span>, email=<span class="hljs-string">&apos;john@doe.com&apos;</span>, password=<span class="hljs-string">&apos;123&apos;</span>)
        self.response = self.client.post(reverse(<span class="hljs-string">&apos;password_reset&apos;</span>), { <span class="hljs-string">&apos;email&apos;</span>: <span class="hljs-string">&apos;john@doe.com&apos;</span> })
        self.email = mail.outbox[<span class="hljs-number">0</span>]

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_email_subject</span><span class="hljs-params">(self)</span>:</span>
        self.assertEqual(<span class="hljs-string">&apos;[Django Boards] Please reset your password&apos;</span>, self.email.subject)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_email_body</span><span class="hljs-params">(self)</span>:</span>
        context = self.response.context
        token = context.get(<span class="hljs-string">&apos;token&apos;</span>)
        uid = context.get(<span class="hljs-string">&apos;uid&apos;</span>)
        password_reset_token_url = reverse(<span class="hljs-string">&apos;password_reset_confirm&apos;</span>, kwargs={
            <span class="hljs-string">&apos;uidb64&apos;</span>: uid,
            <span class="hljs-string">&apos;token&apos;</span>: token
        })
        self.assertIn(password_reset_token_url, self.email.body)
        self.assertIn(<span class="hljs-string">&apos;john&apos;</span>, self.email.body)
        self.assertIn(<span class="hljs-string">&apos;john@doe.com&apos;</span>, self.email.body)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_email_to</span><span class="hljs-params">(self)</span>:</span>
        self.assertEqual([<span class="hljs-string">&apos;john@doe.com&apos;</span>,], self.email.to)
</code></pre>
<p>&#x6B64;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x6293;&#x53D6;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x53D1;&#x9001;&#x7684;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#xFF0C;&#x5E76;&#x68C0;&#x67E5;&#x4E3B;&#x9898;&#x884C;&#xFF0C;&#x6B63;&#x6587;&#x5185;&#x5BB9;&#x4EE5;&#x53CA;&#x53D1;&#x9001;&#x7ED9;&#x8C01;&#x3002;</p>
<h3 id="&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x5B8C;&#x6210;&#x89C6;&#x56FE;">&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x5B8C;&#x6210;&#x89C6;&#x56FE;</h3>
<p><strong>templates/password_reset_done.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base_accounts.html&apos; %}

{% block title %}Reset your password{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-4 col-md-6 col-sm-8&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Reset your password<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Check your email for a link to reset your password. If it doesn&apos;t appear within a few minutes, check your spam folder.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;login&apos; %}&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-secondary btn-block&quot;</span>&gt;</span>Return to log in<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/password_reset_done.jpg" alt="Password Reset Done"></p>
<p><strong>accounts/tests/test_view_password_reset.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth <span class="hljs-keyword">import</span> views <span class="hljs-keyword">as</span> auth_views
<span class="hljs-keyword">from</span> django.core.urlresolvers <span class="hljs-keyword">import</span> reverse
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> resolve
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PasswordResetDoneTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;password_reset_done&apos;</span>)
        self.response = self.client.get(url)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_status_code</span><span class="hljs-params">(self)</span>:</span>
        self.assertEquals(self.response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_view_function</span><span class="hljs-params">(self)</span>:</span>
        view = resolve(<span class="hljs-string">&apos;/reset/done/&apos;</span>)
        self.assertEquals(view.func.view_class, auth_views.PasswordResetDoneView)
</code></pre>
<h3 id="&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x786E;&#x8BA4;&#x89C6;&#x56FE;">&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x786E;&#x8BA4;&#x89C6;&#x56FE;</h3>
<p><strong>templates/password_reset_confirm.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base_accounts.html&apos; %}

{% block title %}
  {% if validlink %}
    Change password for {{ form.user.username }}
  {% else %}
    Reset your password
  {% endif %}
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-6 col-md-8 col-sm-10&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
          {% if validlink %}
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Change password for @{{ form.user.username }}<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
              {% csrf_token %}
              {% include &apos;includes/form.html&apos; %}
              <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success btn-block&quot;</span>&gt;</span>Change password<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
          {% else %}
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Reset your password<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;alert alert-danger&quot;</span> <span class="hljs-attr">role</span>=<span class="hljs-string">&quot;alert&quot;</span>&gt;</span>
              It looks like you clicked on an invalid password reset link. Please try again.
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;password_reset&apos; %}&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-secondary btn-block&quot;</span>&gt;</span>Request a new password reset link<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
          {% endif %}
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#x53EA;&#x80FD;&#x901A;&#x8FC7;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x8BBF;&#x95EE;&#xFF0C;&#x5B83;&#x770B;&#x8D77;&#x6765;&#x50CF;&#x8FD9;&#x6837;&#xFF1A;<a href="http://127.0.0.1:8000/reset/Mw/4po-2b5f2d47c19966e294a1/" target="_blank">http://127.0.0.1:8000/reset/Mw/4po-2b5f2d47c19966e294a1/</a></p>
<p>&#x5728;&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#xFF0C;&#x4ECE;&#x63A7;&#x5236;&#x53F0;&#x4E2D;&#x7684;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&#x83B7;&#x53D6;&#x6B64;&#x94FE;&#x63A5;&#x3002;</p>
<p>&#x5982;&#x679C;&#x94FE;&#x63A5;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF1A;</p>
<p><img src="statics/password_reset_confirm_valid.jpg" alt="Password Reset Confirm Valid"></p>
<p>&#x5018;&#x82E5;&#x94FE;&#x63A5;&#x5DF2;&#x7ECF;&#x88AB;&#x4F7F;&#x7528;&#xFF1A;</p>
<p><img src="statics/password_reset_confirm_invalid.jpg" alt="Password Reset Confirm Invalid"></p>
<p><strong>accounts/tests/test_view_password_reset.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth.tokens <span class="hljs-keyword">import</span> default_token_generator
<span class="hljs-keyword">from</span> django.utils.encoding <span class="hljs-keyword">import</span> force_bytes
<span class="hljs-keyword">from</span> django.utils.http <span class="hljs-keyword">import</span> urlsafe_base64_encode
<span class="hljs-keyword">from</span> django.contrib.auth <span class="hljs-keyword">import</span> views <span class="hljs-keyword">as</span> auth_views
<span class="hljs-keyword">from</span> django.contrib.auth.forms <span class="hljs-keyword">import</span> SetPasswordForm
<span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User
<span class="hljs-keyword">from</span> django.core.urlresolvers <span class="hljs-keyword">import</span> reverse
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> resolve
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PasswordResetConfirmTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        user = User.objects.create_user(username=<span class="hljs-string">&apos;john&apos;</span>, email=<span class="hljs-string">&apos;john@doe.com&apos;</span>, password=<span class="hljs-string">&apos;123abcdef&apos;</span>)

        <span class="hljs-string">&apos;&apos;&apos;
        create a valid password reset token
        based on how django creates the token internally:
        https://github.com/django/django/blob/1.11.5/django/contrib/auth/forms.py#L280
        &apos;&apos;&apos;</span>
        self.uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
        self.token = default_token_generator.make_token(user)

        url = reverse(<span class="hljs-string">&apos;password_reset_confirm&apos;</span>, kwargs={<span class="hljs-string">&apos;uidb64&apos;</span>: self.uid, <span class="hljs-string">&apos;token&apos;</span>: self.token})
        self.response = self.client.get(url, follow=<span class="hljs-keyword">True</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_status_code</span><span class="hljs-params">(self)</span>:</span>
        self.assertEquals(self.response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_view_function</span><span class="hljs-params">(self)</span>:</span>
        view = resolve(<span class="hljs-string">&apos;/reset/{uidb64}/{token}/&apos;</span>.format(uidb64=self.uid, token=self.token))
        self.assertEquals(view.func.view_class, auth_views.PasswordResetConfirmView)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_csrf</span><span class="hljs-params">(self)</span>:</span>
        self.assertContains(self.response, <span class="hljs-string">&apos;csrfmiddlewaretoken&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_contains_form</span><span class="hljs-params">(self)</span>:</span>
        form = self.response.context.get(<span class="hljs-string">&apos;form&apos;</span>)
        self.assertIsInstance(form, SetPasswordForm)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_form_inputs</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        The view must contain two inputs: csrf and two password fields
        &apos;&apos;&apos;</span>
        self.assertContains(self.response, <span class="hljs-string">&apos;&lt;input&apos;</span>, <span class="hljs-number">3</span>)
        self.assertContains(self.response, <span class="hljs-string">&apos;type=&quot;password&quot;&apos;</span>, <span class="hljs-number">2</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidPasswordResetConfirmTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        user = User.objects.create_user(username=<span class="hljs-string">&apos;john&apos;</span>, email=<span class="hljs-string">&apos;john@doe.com&apos;</span>, password=<span class="hljs-string">&apos;123abcdef&apos;</span>)
        uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
        token = default_token_generator.make_token(user)

        <span class="hljs-string">&apos;&apos;&apos;
        invalidate the token by changing the password
        &apos;&apos;&apos;</span>
        user.set_password(<span class="hljs-string">&apos;abcdef123&apos;</span>)
        user.save()

        url = reverse(<span class="hljs-string">&apos;password_reset_confirm&apos;</span>, kwargs={<span class="hljs-string">&apos;uidb64&apos;</span>: uid, <span class="hljs-string">&apos;token&apos;</span>: token})
        self.response = self.client.get(url)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_status_code</span><span class="hljs-params">(self)</span>:</span>
        self.assertEquals(self.response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_html</span><span class="hljs-params">(self)</span>:</span>
        password_reset_url = reverse(<span class="hljs-string">&apos;password_reset&apos;</span>)
        self.assertContains(self.response, <span class="hljs-string">&apos;invalid password reset link&apos;</span>)
        self.assertContains(self.response, <span class="hljs-string">&apos;href=&quot;{0}&quot;&apos;</span>.format(password_reset_url))
</code></pre>
<h3 id="&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x5B8C;&#x6210;&#x89C6;&#x56FE;">&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x5B8C;&#x6210;&#x89C6;&#x56FE;</h3>
<p><strong>templates/password_reset_complete.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base_accounts.html&apos; %}

{% block title %}Password changed!{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row justify-content-center&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-6 col-md-8 col-sm-10&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-body&quot;</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;card-title&quot;</span>&gt;</span>Password changed!<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;alert alert-success&quot;</span> <span class="hljs-attr">role</span>=<span class="hljs-string">&quot;alert&quot;</span>&gt;</span>
            You have successfully changed your password! You may now proceed to log in.
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;login&apos; %}&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-secondary btn-block&quot;</span>&gt;</span>Return to log in<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/password_reset_complete.jpg" alt="Password Reset Complete"></p>
<p><strong>accounts/tests/test_view_password_reset.py</strong> <a href="https://gist.github.com/vitorfs/c9657d39d28c2a0cfb0933e715bfc9cf#file-test_view_password_reset-py-L149" target="_blank">(view complete file contents)</a></p>
<pre><code class="lang-html">from django.contrib.auth import views as auth_views
from django.core.urlresolvers import reverse
from django.urls import resolve
from django.test import TestCase

class PasswordResetCompleteTests(TestCase):
    def setUp(self):
        url = reverse(&apos;password_reset_complete&apos;)
        self.response = self.client.get(url)

    def test_status_code(self):
        self.assertEquals(self.response.status_code, 200)

    def test_view_function(self):
        view = resolve(&apos;/reset/complete/&apos;)
        self.assertEquals(view.func.view_class, auth_views.PasswordResetCompleteView)
</code></pre>
<hr>
<h3 id="&#x5BC6;&#x7801;&#x66F4;&#x6539;&#x89C6;&#x56FE;">&#x5BC6;&#x7801;&#x66F4;&#x6539;&#x89C6;&#x56FE;</h3>
<p>&#x6B64;&#x89C6;&#x56FE;&#x65E8;&#x5728;&#x63D0;&#x4F9B;&#x7ED9;&#x5E0C;&#x671B;&#x66F4;&#x6539;&#x5176;&#x5BC6;&#x7801;&#x7684;&#x767B;&#x5F55;&#x7528;&#x6237;&#x4F7F;&#x7528;&#x3002;&#x901A;&#x5E38;&#xFF0C;&#x8FD9;&#x4E9B;&#x8868;&#x5355;&#x7531;&#x4E09;&#x4E2A;&#x5B57;&#x6BB5;&#x7EC4;&#x6210;&#xFF1A;&#x65E7;&#x5BC6;&#x7801;&#x3001;&#x65B0;&#x5BC6;&#x7801;&#x3001;&#x65B0;&#x5BC6;&#x7801;&#x786E;&#x8BA4;&#x3002;</p>
<p><strong>myproject/urls.py</strong> <a href="https://gist.github.com/vitorfs/0927898f37831cad0d6a4ec538b8a002#file-urls-py-L31" target="_blank">(view complete file contents)</a></p>
<pre><code class="lang-python">url(<span class="hljs-string">r&apos;^settings/password/$&apos;</span>, auth_views.PasswordChangeView.as_view(template_name=<span class="hljs-string">&apos;password_change.html&apos;</span>),
    name=<span class="hljs-string">&apos;password_change&apos;</span>),
url(<span class="hljs-string">r&apos;^settings/password/done/$&apos;</span>, auth_views.PasswordChangeDoneView.as_view(template_name=<span class="hljs-string">&apos;password_change_done.html&apos;</span>),
    name=<span class="hljs-string">&apos;password_change_done&apos;</span>),
</code></pre>
<p>&#x8FD9;&#x4E9B;&#x89C6;&#x56FE;&#x4EC5;&#x9002;&#x5408;&#x767B;&#x5F55;&#x7528;&#x6237;&#xFF0C;&#x4ED6;&#x4EEC;&#x4F7F;&#x7528;&#x540D;&#x4E3A; <code>@login_required</code>&#x7684;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x6B64;&#x88C5;&#x9970;&#x5668;&#x53EF;&#x9632;&#x6B62;&#x975E;&#x6388;&#x6743;&#x7528;&#x6237;&#x8BBF;&#x95EE;&#x6B64;&#x9875;&#x9762;&#x3002;&#x5982;&#x679C;&#x7528;&#x6237;&#x6CA1;&#x6709;&#x767B;&#x5F55;&#xFF0C;Django&#x4F1A;&#x5C06;&#x4ED6;&#x4EEC;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x5728;<strong>settings.py</strong>&#x4E2D;&#x5B9A;&#x4E49;&#x6211;&#x4EEC;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x767B;&#x5F55;URL&#xFF1A;</p>
<p><strong>myproject/settings.py</strong> <a href="https://gist.github.com/vitorfs/2d3a2c45df7deb025b8206c5a9b55e12#file-settings-py-L133" target="_blank">(view complete file contents)</a></p>
<pre><code>LOGIN_URL = &apos;login&apos;
</code></pre><p><strong>templates/password_change.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}Change password{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>Change password<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-lg-6 col-md-8 col-sm-10&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
        {% csrf_token %}
        {% include &apos;includes/form.html&apos; %}
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span>&gt;</span>Change password<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/password_change.png" alt="Change Password"></p>
<p><strong>templates/password_change_done.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}Change password successful{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;password_change&apos; %}&quot;</span>&gt;</span>Change password<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>Success<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;alert alert-success&quot;</span> <span class="hljs-attr">role</span>=<span class="hljs-string">&quot;alert&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Success!<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span> Your password has been changed!
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-secondary&quot;</span>&gt;</span>Return to home page<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/password_change_done.png" alt="Change Password Successful"></p>
<p>&#x5173;&#x4E8E;&#x5BC6;&#x7801;&#x66F4;&#x6539;&#x89C6;&#x56FE;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x7C7B;&#x4F3C;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF0C;&#x5C31;&#x50CF;&#x6211;&#x4EEC;&#x8FC4;&#x4ECA;&#x4E3A;&#x6B62;&#x6240;&#x505A;&#x7684;&#x90A3;&#x6837;&#x3002;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<strong>test_view_password_change.py</strong>&#x7684;&#x65B0;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x6211;&#x5C06;&#x5728;&#x4E0B;&#x9762;&#x5217;&#x51FA;&#x65B0;&#x7684;&#x6D4B;&#x8BD5;&#x7C7B;&#x578B;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x6211;&#x4E3A;&#x5BC6;&#x7801;&#x66F4;&#x6539;&#x89C6;&#x56FE;&#x7F16;&#x5199;&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#xFF0C;&#x7136;&#x540E;&#x5355;&#x51FB;&#x4EE3;&#x7801;&#x6BB5;&#x65C1;&#x8FB9;&#x7684;&#x67E5;&#x770B;&#x6587;&#x6B63;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x94FE;&#x63A5;&#x3002;&#x5927;&#x90E8;&#x5206;&#x6D4B;&#x8BD5;&#x4E0E;&#x6211;&#x4EEC;&#x8FC4;&#x4ECA;&#x4E3A;&#x6B62;&#x6240;&#x505A;&#x7684;&#x76F8;&#x4F3C;&#x3002;&#x6211;&#x8F6C;&#x79FB;&#x5230;&#x4E00;&#x4E2A;&#x5916;&#x90E8;&#x6587;&#x4EF6;&#x4EE5;&#x907F;&#x514D;&#x592A;&#x8FC7;&#x4E8E;&#x590D;&#x6742;&#x3002;</p>
<p><strong>accounts/tests/test_view_password_change.py</strong> <a href="https://gist.github.com/vitorfs/03e2f20a4c1e693c9b22b343503fb461#file-test_view_password_change-py-L40" target="_blank">(view complete file contents)</a></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginRequiredPasswordChangeTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_redirection</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;password_change&apos;</span>)
        login_url = reverse(<span class="hljs-string">&apos;login&apos;</span>)
        response = self.client.get(url)
        self.assertRedirects(response, f<span class="hljs-string">&apos;{login_url}?next={url}&apos;</span>)
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x6D4B;&#x8BD5;&#x5C1D;&#x8BD5;&#x8BBF;&#x95EE;<strong>password_change</strong>&#x89C6;&#x56FE;&#x800C;&#x4E0D;&#x767B;&#x5F55;&#x3002;&#x9884;&#x671F;&#x7684;&#x884C;&#x4E3A;&#x662F;&#x5C06;&#x7528;&#x6237;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;&#x3002;</p>
<p><strong>accounts/tests/test_view_password_change.py</strong> <a href="https://gist.github.com/vitorfs/03e2f20a4c1e693c9b22b343503fb461#file-test_view_password_change-py-L48" target="_blank">(view complete file contents)</a></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PasswordChangeTestCase</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self, data={})</span>:</span>
        self.user = User.objects.create_user(username=<span class="hljs-string">&apos;john&apos;</span>, email=<span class="hljs-string">&apos;john@doe.com&apos;</span>, password=<span class="hljs-string">&apos;old_password&apos;</span>)
        self.url = reverse(<span class="hljs-string">&apos;password_change&apos;</span>)
        self.client.login(username=<span class="hljs-string">&apos;john&apos;</span>, password=<span class="hljs-string">&apos;old_password&apos;</span>)
        self.response = self.client.post(self.url, data)
</code></pre>
<p>&#x5728;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<strong>PasswordChangeTestCase </strong>&#x7684;&#x65B0;&#x7C7B;&#x3002;&#x5B83;&#x5C06;&#x8FDB;&#x884C;&#x57FA;&#x672C;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x5E76;&#x5411; <strong>password_change</strong>&#x89C6;&#x56FE;&#x53D1;&#x9001;&#x4E00;&#x4E2A;<strong>POST</strong> &#x8BF7;&#x6C42;&#x3002;&#x5728;&#x4E0B;&#x4E00;&#x7EC4;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x7C7B;&#x800C;&#x4E0D;&#x662F; <strong>TestCase</strong>&#x7C7B;&#x6765;&#x6D4B;&#x8BD5;&#x6210;&#x529F;&#x8BF7;&#x6C42;&#x548C;&#x65E0;&#x6548;&#x8BF7;&#x6C42;&#xFF1A;</p>
<p><strong>accounts/tests/test_view_password_change.py</strong> <a href="https://gist.github.com/vitorfs/03e2f20a4c1e693c9b22b343503fb461#file-test_view_password_change-py-L60" target="_blank">(view complete file contents)</a></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SuccessfulPasswordChangeTests</span><span class="hljs-params">(PasswordChangeTestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        super().setUp({
            <span class="hljs-string">&apos;old_password&apos;</span>: <span class="hljs-string">&apos;old_password&apos;</span>,
            <span class="hljs-string">&apos;new_password1&apos;</span>: <span class="hljs-string">&apos;new_password&apos;</span>,
            <span class="hljs-string">&apos;new_password2&apos;</span>: <span class="hljs-string">&apos;new_password&apos;</span>,
        })

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_redirection</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        A valid form submission should redirect the user
        &apos;&apos;&apos;</span>
        self.assertRedirects(self.response, reverse(<span class="hljs-string">&apos;password_change_done&apos;</span>))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_password_changed</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        refresh the user instance from database to get the new password
        hash updated by the change password view.
        &apos;&apos;&apos;</span>
        self.user.refresh_from_db()
        self.assertTrue(self.user.check_password(<span class="hljs-string">&apos;new_password&apos;</span>))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_user_authentication</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        Create a new request to an arbitrary page.
        The resulting response should now have an `user` to its context, after a successful sign up.
        &apos;&apos;&apos;</span>
        response = self.client.get(reverse(<span class="hljs-string">&apos;home&apos;</span>))
        user = response.context.get(<span class="hljs-string">&apos;user&apos;</span>)
        self.assertTrue(user.is_authenticated)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InvalidPasswordChangeTests</span><span class="hljs-params">(PasswordChangeTestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_status_code</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        An invalid form submission should return to the same page
        &apos;&apos;&apos;</span>
        self.assertEquals(self.response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_form_errors</span><span class="hljs-params">(self)</span>:</span>
        form = self.response.context.get(<span class="hljs-string">&apos;form&apos;</span>)
        self.assertTrue(form.errors)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_didnt_change_password</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        refresh the user instance from the database to make
        sure we have the latest data.
        &apos;&apos;&apos;</span>
        self.user.refresh_from_db()
        self.assertTrue(self.user.check_password(<span class="hljs-string">&apos;old_password&apos;</span>))
</code></pre>
<p><code>refresh_from_db()</code>&#x65B9;&#x6CD5;&#x786E;&#x4FDD;&#x6211;&#x4EEC;&#x62E5;&#x6709;&#x6700;&#x65B0;&#x7684;&#x6570;&#x636E;&#x72B6;&#x6001;&#x3002;&#x5B83;&#x5F3A;&#x5236;Django&#x518D;&#x6B21;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#x5E93;&#x4EE5;&#x66F4;&#x65B0;&#x6570;&#x636E;&#x3002;&#x8003;&#x8651;&#x5230;<strong>change_password</strong>&#x89C6;&#x56FE;&#x4F1A;&#x66F4;&#x65B0;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x5BC6;&#x7801;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x8FD9;&#x6837;&#x505A;&#x3002;&#x4E3A;&#x4E86;&#x67E5;&#x770B;&#x6D4B;&#x8BD5;&#x5BC6;&#x7801;&#x662F;&#x5426;&#x771F;&#x7684;&#x6539;&#x53D8;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x83B7;&#x53D6;&#x6700;&#x65B0;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<hr>
<h3 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h3>
<p>&#x5BF9;&#x4E8E;&#x5927;&#x591A;&#x6570;Django&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#xFF0C;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x662F;&#x4E00;&#x79CD;&#x975E;&#x5E38;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x4F8B;&#x3002;&#x5728;&#x672C;&#x6559;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x4E86;&#x6240;&#x6709;&#x91CD;&#x8981;&#x89C6;&#x56FE;&#xFF1A;&#x6CE8;&#x518C;&#x3001;&#x767B;&#x5F55;&#x3001;&#x6CE8;&#x9500;&#x3001;&#x5BC6;&#x7801;&#x91CD;&#x7F6E;&#x548C;&#x66F4;&#x6539;&#x5BC6;&#x7801;&#x3002;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6709;&#x4E86;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x6765;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x5E76;&#x8FDB;&#x884C;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x80FD;&#x591F;&#x7EE7;&#x7EED;&#x5F00;&#x53D1;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x548C;&#x5176;&#x4ED6;&#x89C6;&#x56FE;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4ECD;&#x7136;&#x9700;&#x8981;&#x6539;&#x8FDB;&#x5F88;&#x591A;&#x5173;&#x4E8E;&#x4EE3;&#x7801;&#x8BBE;&#x8BA1;&#x7684;&#x95EE;&#x9898;&#xFF1A;&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x5939;&#x5F00;&#x59CB;&#x53D8;&#x5F97;&#x4E71;&#x4E03;&#x516B;&#x7CDF;&#x3002; <strong>boards</strong> &#x5E94;&#x7528;&#x6D4B;&#x8BD5;&#x4ECD;&#x7136;&#x662F;&#x6DF7;&#x4E71;&#x7684;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x5F00;&#x59CB;&#x91CD;&#x6784;&#x65B0;&#x7684;&#x4E3B;&#x9898;&#x89C6;&#x56FE;&#xFF0C;&#x56E0;&#x4E3A;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x68C0;&#x7D22;&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x3002;&#x6211;&#x4EEC;&#x5F88;&#x5FEB;&#x5C31;&#x5C06;&#x505A;&#x5230;&#x8FD9;&#x4E00;&#x70B9;&#x3002;</p>
<p>&#x6211;&#x5E0C;&#x671B;&#x4F60;&#x559C;&#x6B22;&#x672C;&#x6559;&#x7A0B;&#x7CFB;&#x5217;&#x7684;&#x7B2C;&#x56DB;&#x90E8;&#x5206;&#xFF01;&#x7B2C;&#x4E94;&#x90E8;&#x5206;&#x5C06;&#x4E8E;2017&#x5E74;10&#x6708;2&#x65E5;&#x4E0B;&#x5468;&#x53D1;&#x5E03;&#xFF0C;&#x5982;&#x679C;&#x60A8;&#x5E0C;&#x671B;&#x5728;&#x7B2C;&#x4E94;&#x90E8;&#x5206;&#x7ED3;&#x675F;&#x7684;&#x65F6;&#x5019;&#x6536;&#x5230;&#x901A;&#x8FC7;&#xFF0C;&#x8BF7;&#x60A8;&#x8BA2;&#x9605;&#x6211;&#x4EEC;&#x7684;&#x90AE;&#x4EF6;&#x5217;&#x8868;&#x3002;</p>
<p>&#x8BE5;&#x9879;&#x76EE;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x5728;GitHub&#x4E0A;&#x9762;&#x53EF;&#x7528;&#xFF0C;&#x9879;&#x76EE;&#x7684;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x5728;&#x53D1;&#x5E03;&#x6807;&#x7B7E;<strong>v0.4-lw</strong>&#x4E0B;&#x53EF;&#x4EE5;&#x627E;&#x5230;&#x3002;&#x94FE;&#x63A5;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><a href="https://github.com/sibtc/django-beginners-guide/tree/v0.4-lw" target="_blank">https://github.com/sibtc/django-beginners-guide/tree/v0.4-lw</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Authentication1.html" class="navigation navigation-prev " aria-label="Previous page: Django入门与实践-第15章：用户注销">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="DjangoORM0.html" class="navigation navigation-next " aria-label="Next page: Django入门与实践-第17章：保护视图">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Django入门与实践-第16章：用户登录","level":"1.17","depth":1,"next":{"title":"Django入门与实践-第17章：保护视图","level":"1.18","depth":1,"path":"DjangoORM0.md","ref":"./DjangoORM0.md","articles":[]},"previous":{"title":"Django入门与实践-第15章：用户注销","level":"1.16","depth":1,"path":"Authentication1.md","ref":"./Authentication1.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["include-codeblock","livereload"],"pluginsConfig":{"include-codeblock":{"check":false,"edit":false,"fixlang":false,"lang":"","template":"default","theme":"chrome","unindent":false},"livereload":{},"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"fontsettings":{"family":"sans","size":2,"theme":"white"},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"file":{"path":"Authentication2.md","mtime":"2018-05-13T13:48:46.856Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-05-13T12:52:49.928Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

